# -*- coding: UTF-8 -*-
# Spider for 'asos.com'
#Author: Denis <sinedone@gmail.com>,

from urlparse import urljoin

from scrapy.spider import BaseSpider
from scrapy.http import  Request
from scrapy.selector import HtmlXPathSelector
from littlecrawler.items import ScrapedItem

class zara(BaseSpider):

    name = 'zara'
    start_urls = ['http://www.zara.com/webapp/wcs/stores/servlet/category/uk/en/zara-S2011/61002/Woman']
    URL = 'http://www.zara.com'
    visited = set()
    
    def parse(self, response):
        page = HtmlXPathSelector(response)
        for cat in page.select("//ul[@class='menuLevel3']//a/@href").extract():
            yield Request(self.URL+cat, callback=self.parse)
        else:
            for i in self.list_items(response):
                yield i
            
    def list_items(self, response):
        page = HtmlXPathSelector(response)
        for url in page.select("//ul[@id='catProdsContainer']//a[@class='mainProductLink pLink']/@href").extract():
            if url not in self.visited:
                self.visited.add(url)
                item = ScrapedItem()
                item['url'] = urljoin(response.url, url)
                yield item
#                yield Request(self.URL+item)
        next_page = page.select("//li[@class='arrow']/a[@class='next pagLink ']/@href").extract()
        if next_page:
            yield Request(urljoin(response.url, next_page[0]), callback=self.list_items)
        
        